//
//  ZSSearchView.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/6.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit
@IBDesignable
class ZSSearchView: UIView {

    @IBOutlet weak var consRight: NSLayoutConstraint!
  
    @IBOutlet weak var txtView: UITextField!

    
    @IBOutlet weak var btnCancel: UIButton!

    
    @IBAction func beginEditing(sender: AnyObject) {
    print("开始编辑")
        /**
         开始编辑的时候,让文本框的右边的约束值进行改变,不再遮住按钮
        */
        
        self.consRight.constant = btnCancel.frame.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()

        }
  }

    
    
    //点击取消的方法
    @IBAction func btnCancelDidClick(sender: AnyObject) {
    
        self.consRight.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
        self.endEditing(true)
        
    }
    

    
    //提供一个类方法,供外部调用,创建一个searchView
    class func loadSearchView()->(ZSSearchView) {
    return NSBundle.mainBundle().loadNibNamed("searchView", owner: nil, options: nil).last as! ZSSearchView
    }
    
    
    /**
     初始化方法,初始化一些特殊的
     */
    override func awakeFromNib() {

        frame = CGRectMake(0,0,UIScreen.mainScreen().bounds.width,30);
        
        let imageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        self.txtView.leftView = imageView

        self.txtView.leftView?.frame = CGRect(x: 0, y: 0, width: self.bounds.height, height: self.bounds.height)
        
        self.txtView.leftView?.contentMode = .Center

        self.txtView.leftViewMode = .Always

//        self.txtView.layer.cornerRadius = 18
//        self.txtView.layer.masksToBounds = true
    }
    
    
    
    
    
    
}
